<?php

namespace app\scm\controller;

use app\admin\common\Base;
use think\Db;
use app\scm\model\Invpu as Model;
use app\scm\model\InvpuGoods as GoodsModel;
use app\scm\model\PurchaseSalesList as PSLModel;

class Purchasesaleslist extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
        
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map == '') {
                $list = Db::name('purchase_sales_list')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('purchase_sales_list')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('purchase_sales_list')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('purchase_sales_list')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }

            foreach ($list as $k => &$v) {
                $v['customer'] = Db::name('customer')->where('id', $v['customer_id'])->value('name');
                $v['money_type'] = Db::name('money_type')->where('id', $v['money_type_id'])->value('name');
                // $v['unpaid_amount'] = $v['price'];
                $table = $v['type'] == 0 ? 'invpu' : 'order';
                $v['sn'] = Db::name($table)->where('id', $v['pid'])->value('sn');
                $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
                $v['enterprise'] = Db::name('enterprise')->where('id', $this->EnterpriseId)->value('title');
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }
}
